<?php
namespace Realisation\DataProvider;

/**
 * Description of ApiDataProvider
 *
 * @author pahhan
 */
class ApiDataProvider extends AbstractDataProvider
{

    /**
     *
     * @return Doctrine\DBAL\Statement
     */
    public function getSites()
    {
        $query = 'SELECT * FROM site';
        $stmt = $this->conn->executeQuery($query);
        return $stmt;
    }

    /**
     *
     * @return Doctrine\DBAL\Statement
     */
    public function getUrls()
    {
        $query = 'SELECT
                id,
                site_id,
                title,
                url,
                description,
                updated_at
            FROM url';
        $stmt = $this->conn->executeQuery($query);
        return $stmt;
    }

    /**
     *
     * @param integer $url_id
     * @param integer $from_id
     * @param integer $limit
     * @return Statement
     */
    public function getMessages($url_id, $from_id, $limit)
    {
        $query = '
            SELECT
                id,
                url_id,
                external_id,
                published_at,
                title,
                content,
                tags
            FROM message
            WHERE url_id = :url_id
            AND id > :from_id
            ORDER BY id
            LIMIT '.$limit;
        $params = array(
            'url_id' => $url_id,
            'from_id' => $from_id,
        );

        $types = array(\PDO::PARAM_INT, \PDO::PARAM_INT);
        $stmt = $this->conn->executeQuery($query, $params, $types);
        return $stmt;
    }

    public function getMessagesCount($url_id, $from_id)
    {
        $query = '
            SELECT COUNT(id)
            FROM message
            WHERE url_id = :url_id
            AND id > :from_id';
        $params = array(
            'url_id' => $url_id,
            'from_id' => $from_id,
        );

        $types = array(\PDO::PARAM_INT, \PDO::PARAM_INT);
        $stmt = $this->conn->executeQuery($query, $params, $types);
        return $stmt;
    }
}

